#include "StdAfx.h"
#include "SGFile.h"




HD_CstrHstr  Dll_ch;
 HD_HstrCstr  Dll_hc;
HD_ApduT0 Dll_at;
HD_DstrHstr Dll_dh;

 BOOL File_InitInstance()
 {
	if(Algorithm_hDLL == NULL)
		Algorithm_hDLL=LoadLibrary((LPCTSTR)"HDMATH20B.dll");
	printf("MATH DLL %d",Algorithm_hDLL);
	if(Algorithm_hDLL)
	{
		Dll_ch =(HD_CstrHstr)GetProcAddress(Algorithm_hDLL,"HD_CstrHstr");
		Dll_hc= (HD_HstrCstr)GetProcAddress(Algorithm_hDLL,"HD_HstrCstr");
		Dll_dh =(HD_DstrHstr)GetProcAddress(Algorithm_hDLL,"HD_DstrHstr");
	}
	else
	{
		return false;
	}
	if(Reader_hDLL == NULL)
	{
		HANDLE h1 = LoadLibrary((LPCTSTR)"dcic32.dll");
		Reader_hDLL=LoadLibrary((LPCTSTR)"HDIFD20B.dll");
	}
	
	if(Reader_hDLL)
	{
		Dll_at =(HD_ApduT0)GetProcAddress(Reader_hDLL,"HD_ApduT0");
	}
	return true;
 }
int ReadCapFile(unsigned char* fileout)
{
	FILE	*fin;
	fin = fopen(PBOC_FILE_INPUT_SAMDATE,"rb");
	printf("FILE HANDLE %d\n",fin);
	int offset = 0;
	while (!feof (fin))
	{
		offset+=fread (fileout+offset,1,256,fin);
	}
	//printf("FILE Content: %s\n",fileout);
	fclose (fin);
	return offset;
}

int CstrToHstr(
	unsigned char *svChar,
	short svCharLen,
	short ivNum,
	char *srHex
){
	return Dll_ch(svChar,svCharLen,ivNum,srHex);
}
//unsigned short HstrCstr (
//	char *svHex,
//	unsigned char *srChar
//)
//{
//	 return Dll_hc(svHex,srChar);
//}
int  CharToByte (int len, char *pcInDate,unsigned char *pcOutDate)
{
	int i = 0;
	int j = 0;
	char *buffer = NULL;
	buffer = new char[(len+1)*sizeof(char)];
	buffer[len] = '\0';
	
	for (i = 0; i < len; i++)
	{
		if ('0' <= pcInDate[i] && '9' >= pcInDate[i])
		{
			buffer[i] = pcInDate[i] - '0';
		}
		else if ('a' <= pcInDate[i] && 'f' >= pcInDate[i])
		{
			buffer[i] = pcInDate[i] - 'a' +10;
		}
		else if ('A' <= pcInDate[i] && 'F' >= pcInDate[i])
		{
			buffer[i] = pcInDate[i] - 'A' +10;
		}
		else
		{
			return 0xff;
		}
	}

	for (i=0;i<len/2;i++)
	{
		j = 2*i;
		pcOutDate[i] = (buffer[j] << 4) + buffer[j+1];
	}
	pcOutDate[len/2] = '\0';

	delete[] buffer;
	return 0;
}
unsigned short DstrHstr (
	char *svDec,
	short ivNum,
	char *srHex
){
	return Dll_dh(svDec,ivNum,srHex);
}
